﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using SYcwpb.ViewModels;
using System.ComponentModel;
using SYcwpb.Helpers;

namespace SYcwpb.Controls
{
    public enum DriverType
    {
        IsOK=0,
        IsDaoBan=1,
        IsOverTime=2
    }

    /// <summary>
    /// DriverCard.xaml 的交互逻辑
    /// </summary>
    public partial class DriverCard : UserControl
    {
        public static Dictionary<int, string> DicDriverGroup = DriverCard.InitDicGroupType();

        /// <summary>
        /// 员工ID
        /// </summary>
        public string Driverid { get; set; }

        /// <summary>
        /// 替换员工名
        /// </summary>
        public string ReplaceName { get; set; }

        /// <summary>
        /// 替换员工ID
        /// </summary>
        public string ReplaceDriverId { get; set; }


        public int DriverGroupId { get; set; }

        private string _jobNum;
        /// <summary>
        /// 员工工号
        /// </summary>
        public string JobNum
        {
            get { return _jobNum; }
            set
            {
                if (value == _jobNum)
                {
                    return;
                }
                _jobNum = value;

                //SetImag(GlobalVariables.LoadHeadImage(_jobNum));
            }
        }

        private SolidColorBrush _defaultBackground = Brushes.Gold;
        private SolidColorBrush _switchBackground = Brushes.LightYellow;

        /// <summary>
        /// 司机姓名
        /// </summary>
        public string DriverName
        {
            get
            {
                return tbk_name.Text; 
            }
            set {
                tbk_name.Text = value;
            } 
        }

        /// <summary>
        /// 轮值序号
        /// </summary>
        public string DutyNum
        {
            get
            {
                return tbk_dutyNum.Text;
            }
            set
            {
                tbk_dutyNum.Text = value;
            } 
        }

        public string DutyNO
        {
            get
            {
                return tbk_dutyNo.Text;
            }
            set
            {
                tbk_dutyNo.Text = value;
            }
        }

        /// <summary>
        /// 交路号
        /// </summary>
        public string CrossNum
        {
            get
            {
                return tbk_crNum.Text;
            }
            set
            {
                tbk_crNum.Text = value;
            }
        }

        /// <summary>
        /// string 司机类型：热备、调车、试车线、备用
        /// </summary>
        public string DType
        {
            get
            {
                return tbk_type.Text;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    tbk_type.Text = "";
                }
                else
                {
                    string content;
                    if (DicDriverGroup.TryGetValue(Convert.ToInt32(value), out content))
                    {
                        tbk_type.Text = content;
                    }
                }
                
            }
        }

        private string _switchid;
        /// <summary>
        /// 调班司机的SwitchRecord表ID
        /// </summary>
        public string SwitchID
        {
            get { return _switchid; }
            set
            {
                if (_switchid == value)
                    return;

                _switchid = value;

                if (string.IsNullOrEmpty(_switchid))
                {
                    this.myGrid.Background = _defaultBackground;
                }
                else
                    this.myGrid.Background = _switchBackground;
            }
        }

        /// <summary>
        /// 早班，白班，夜班，休班
        /// </summary>
        public string Driver_group { get; set; }

        private Image HeadImag { get; set; }

        private ToolTip HeadTip { get; set; }

        public string ImagPath { get; set; }

        public static Dictionary<int, string> InitDicGroupType()
        {
            DicDriverGroup = new Dictionary<int, string>();
            DicDriverGroup.Add(1, "热备");
            DicDriverGroup.Add(2, "调车");
            DicDriverGroup.Add(3, "试车线");
            DicDriverGroup.Add(4, "备班");
            DicDriverGroup.Add(5, "");
            DicDriverGroup.Add(6, "月备");
            DicDriverGroup.Add(7, "司机长");
            DicDriverGroup.Add(8,"督导员");

            return DicDriverGroup;
        }

        public DriverCard()
        {
            InitializeComponent();

            //InitImag();
            //HeadTip = new ToolTip();

            this.DataContext = this;
        }


        public void ReInit()
        {
            this.DriverName = null;
            this.Driverid = null;

            this.ImagPath = null;
            HeadImag.Source = null;
            HeadTip.Content = null;
            this.ToolTip = null;

        }

        /// <summary>
        /// 返回string 类型对应的 int ID
        /// </summary>
        /// <returns></returns>
        public int GetIntType()
        {
            int result = -1;
            for (int i = 0; i < DicDriverGroup.Count;i++ )
            {
                if (DicDriverGroup[i] == this.DType)
                    result = i;
            }
            return result;
        }

        private void InitImag()
        {
            HeadImag = new Image();
            HeadImag.Height = 160;
            HeadImag.Width = 120;
        }

        public void SetImag(string path)
        {
            ImagPath = path;
            if (ImagPath == null)
            {
                HeadImag.Source = null;
                HeadTip.Content = null;
                this.ToolTip = null;
                return;
            }
            BitmapImage Bimag = new BitmapImage(new Uri(ImagPath));
            HeadImag.Source = Bimag;

            HeadTip.Content = HeadImag;
            this.ToolTip = HeadTip;
            this.HeadTip.Background = new SolidColorBrush(Colors.Transparent);
        }

        public void SetCaptain()
        {
            this.myGrid.Background = new SolidColorBrush(Colors.OrangeRed);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (null != handler)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        public void Copy(DriverCard card)
        {
            if (card == null)
            {
                this.Driverid =
                this.JobNum =
                this.DriverName =
                this.ReplaceName =
                this.DutyNum =
                this.CrossNum =
                this.tbk_type.Text =
                this.Driver_group = string.Empty;
                return;
            }
            this.Driverid = card.Driverid;
            this.JobNum = card.JobNum;
            this.DriverName = card.DriverName;
            this.ReplaceName = card.ReplaceName;
            this.DutyNum = card.DutyNum;
            this.CrossNum = card.CrossNum;
            this.tbk_type.Text = card.DType;
            this.Driver_group = card.Driver_group;
        }
        public void Slimming()
        {
            CardRow0.Visibility = Visibility.Collapsed;
            rpl_name.Visibility = Visibility.Visible;
            tbk_name.FontSize = 14;
        }
        public void Highlight()
        {
            myGrid.Background = Brushes.LightGoldenrodYellow;
        }
        public void CancelHightlight()
        {
            myGrid.Background = Brushes.Gold;
        }
        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           // MessageBox.Show("");
        }

        private void StackPanel_MouseWheel(object sender, MouseWheelEventArgs e)
        {

        }
    }
}
